package com.supermap.dataservice.disaster;


import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.disaster.dao.DisasterDIPDao;
import com.supermap.dataservice.disaster.dao.DisasterDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.disaster.DisasterDIPService;
import com.supermap.dubbo.api.disaster.DisasterService;
import com.supermap.dubbo.api.disaster.entity.Disaster;
import com.supermap.dubbo.api.disaster.entity.DisasterComment;
import com.supermap.dubbo.api.disaster.entity.DisasterDIP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:病虫害信息业务逻辑层
 * @Date:18/7/31 下午3:01
 */
@Service
public class DisasterDIPServiceImpl implements DisasterDIPService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterDIPServiceImpl.class);

    @Resource
    private DisasterDIPDao disasterDIPDao;

    @Override
    public JSONUtil getDisasterDIPByUIdArea(String userId,String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            if(areaCode!=null){
                areaCode=areaCode+"%";
            }
            //获得用户病虫害信息
            List<DisasterDIP> disasterDIPS = disasterDIPDao.getDisasterDIPByUIdArea(userId,areaCode);

            //获得用户病虫害信息后返回的json数据
            jsonUtil.setMsg("Get user disasterDIPS success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterDIPS);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterDIPServiceImpl=>getDisasterDIPByUIdArea=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get user disasterDIPS flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addDisasterDIP(DisasterDIP disasterDIP) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //增加病虫害信息
           disasterDIPDao.addDisasterDIP(disasterDIP);

            //增加病虫害信息后返回的json数据
            jsonUtil.setMsg("add user disasterDIP success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterDIP);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterDIPServiceImpl=>addDisasterDIP=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("add user disasterDIP flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllDisasterDIP() {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得用户所有的病虫害信息«
             List<DisasterDIP> disasterDIPS = disasterDIPDao.getAllDisasterDIP();

            //获得用户所有的病虫害信息后返回的json数据
            jsonUtil.setMsg("Get all disasterdip success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterDIPS);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterDIPServiceImpl=>getAllDisasterDIP=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all disasterdip flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delDisasterDIP(int disasterdipId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //删除病虫害信息
            disasterDIPDao.delDisasterDIP(disasterdipId);

            //删除病虫害信息后返回的json数据
            jsonUtil.setMsg("del disasterdip success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterdipId);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterDIPServiceImpl=>delDisasterDIP=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("del disasterdip flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getDisasterDIPByareacode(String areacode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<DisasterDIP> disasterDIPList= disasterDIPDao.getDisasterByareacode(areacode);
            //通过areacode获得用户所有的病虫害信息后返回的json数据
            jsonUtil.setMsg("Get all disasterdip by areacode success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterDIPList);
        }catch (Exception e){
            logger.error("DisasterDIPServiceImpl=>getDisasterDIPByareacode=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all disasterdip  by areacode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
    @Override
    public JSONUtil getDisasterDIP(String type,String areaCode,String disasterType,String dateTime) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<DisasterDIP> disasterDIPS = disasterDIPDao.getDisasterDIP(type,areaCode,disasterType,dateTime);
            jsonUtil.setMsg("getDisasterdipByCropId success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterDIPS);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterDIPServiceImpl=>getDisasterDIP=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getDisasterdipByCropId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

}
